﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace ChessModel
{
    /// <summary>Zero-based location on a board.</summary>
    public struct Coordinate
    {
        private int x;
        private int y;

        public Coordinate (int newX, int newY)
        { x = newX; y = newY; }

        public int X { get { return x; } }
        public int Y { get { return y; } }

        public override string ToString ()
        {
            string s = char.ConvertFromUtf32 (((int) 'a') + x);
            s += (y + 1).ToString ();
            return s;
        }
    }


    /// <summary>Represent the board as a two-dimensional array.</summary>
    /// <remarks>The board consists of a two-dimensional array of Squares.
    /// Each Square may be empty (a Square) or may be an instance of a class
    /// that derives from Piece.</remarks>
    public class Chessboard
    {
        internal Square[,] squares;

        // Soft-code the dimensions of the board.
        public int XSize { get { return squares.GetUpperBound (0) + 1; } }
        public int YSize { get { return squares.GetUpperBound (1) + 1; } }

        public Square this[int x, int y]
        { get { return squares[x, y]; } }


        /// <summary>Create a new board.</summary>
        /// <remarks>This constructor contains the only hard-coded size of the board.</remarks>
        internal Chessboard ()
        {
            squares = new Square[8, 8];
            Clear ();
        }


        internal void Clear ()
        {
            for (int x = 0; x < XSize; ++x)
                for (int y = 0; y < YSize; ++y)
                    squares[x, y] = Square.Empty;
        }


        /// <summary>Produce a text representation of the current board.</summary>
        /// <remarks>The text representation of the board is useful for bench
        /// testing and is not by any GUI.</remarks>
        /// <returns>The current board in text form.</returns>
        public override string ToString ()
        {
            string result = "";

            for (int y = YSize - 1; ; --y)
            {
                for (int x = 0; ; ++x)
                {
                    string s = String.Format ("{0}", squares[x, y].ToString ());
                    result += s;

                    if (x >= XSize - 1)
                        break;

                    result += " ";
                }

                if (y <= 0)
                    return result;

                result += "\n";
            }
        }
    }
}
